import { Injectable } from '@angular/core';
import * as g from '../global/globalconst';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import * as gc from 'src/app/global/globalconst';


export class TestMsg{
  msg:TemplateMsg = new TemplateMsg();
  openIds:string[] = [];
}

/**
 * 属性名称不可修改！！！
 * 属性的名称是对应微信官方提供的消息结构体
 */
export class TemplateMsg{
  data:MsgData = new MsgData();
  private miniprogram:MiniProgram = new MiniProgram();

  touser:string;
  private template_id:string = '3lbb6yHXF-hmoWqq0xfLQ00djRXX-DwUqZUCNV-n2Jc';
  url:string;

  constructor(){
    this.miniprogram.appid = "";
    this.miniprogram.pagepath = "";
  }
}

export class MiniProgram{
  appid:string = "";pagepath:string = "";
}

export class MsgData{
  first:MetaData = new MetaData();
  keyword1:MetaData = new MetaData();
  keyword2:MetaData = new MetaData();
  keyword3:MetaData = new MetaData();
  keyword4:MetaData = new MetaData();
  keyword5:MetaData = new MetaData();
  remark:MetaData = new MetaData();
}


export class MetaData{
  value:string;
  color:string = g.TEMPLATE_MSG_COLOR_GENERAL;

  constructor(val:string=''){
    this.value = val;
  }
}


/**
 * template message service
 * 模板消息服务类
 */
@Injectable({
  providedIn: 'root'
})
export class TmsgService {

  constructor(
    private http:HttpClient
  ) { }

  send(msg:TemplateMsg,openIds:string):Observable<any>{
    const openIdArray = openIds.split(",");
    let tmsg:TestMsg = new TestMsg();
    tmsg.msg = msg;
    tmsg.openIds = openIdArray;

    const url = 'http://localhost:7443/dev/pushMsgTest';
    console.log('发送测试模板消息的url = ' + url);
    
    console.log('消息体的参数是：');
    console.log(tmsg);
    
    
    return this.http.post(url,tmsg);
  }


  sendTmpDtl(url:string='',openId:string='ovC8bs78-EymCeGDgtycJKOXGTh0'){
    const openIds = 'ovC8bs78-EymCeGDgtycJKOXGTh0';
    let msg:TemplateMsg = new TemplateMsg();
    // msg.url='https://www.xdfznh.club/wxpadfe/soDtlt?billCode=F191019008';
    if(url.length>0){
      msg.url = url;
    }else{
      msg.url = '';
    }
    msg.touser='';
    msg.data.first=new MetaData('11');
    msg.data.keyword1 = new MetaData('22');
    msg.data.keyword2 = new MetaData('33');
    msg.data.keyword3 = new MetaData('33');
    msg.data.keyword4 = new MetaData('33');
    msg.data.keyword5 = new MetaData('33');
    msg.data.remark = new MetaData('这里是备注');

    this.send(msg,openIds).subscribe(
      ( res:any ) =>{
        console.log('发送成功！');
      },error => console.log('发送测试模板消息时出错！')
    )
    console.log('发送完毕');
  }

  doSend(){
    console.log("进入了doSend");
    
    const openIds = 'ovC8bs78-EymCeGDgtycJKOXGTh0';
    let msg:TemplateMsg = new TemplateMsg();
    msg.url='https://www.xdfznh.club/wxpadfe/test';
    msg.touser='';
    msg.data.first=new MetaData('11');
    msg.data.keyword1 = new MetaData('22');
    msg.data.keyword2 = new MetaData('33');
    msg.data.keyword3 = new MetaData('33');
    msg.data.keyword4 = new MetaData('33');
    msg.data.keyword5 = new MetaData('33');
    msg.data.remark = new MetaData('这里是备注');

    this.send(msg,openIds).subscribe(
      ( res:any ) =>{
        console.log('发送成功！');
      },error => console.log('发送测试模板消息时出错！')
    )
    console.log('发送完毕');
    
  }
}
